using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace OpenComposite.EII.Controls.DataGridViewColumns
{
	[ToolboxItem(false)]
	public partial class UCSelectWebServiceMethod : UserControl
	{
		#region Initialization
		public UCSelectWebServiceMethod()
		{
			InitializeComponent();
		}
		#endregion

		#region Public Members

		#region Properties
		public SelectedWebServiceMethod Value
		{
			get {
				return _selectedWSM;
			}
			set {
				if ( _selectedWSM != value ) {
					_selectedWSM = value;
					showSelectedWebServiceMethod();
					OnValueChanged();
				}
			}
		}
		#endregion

		#region Events
		public event EventHandler ValueChanged = null;
		#endregion

		#region Methods
		#endregion

		#endregion

		#region Protected Members

		#region Methods
		protected virtual void OnValueChanged()
		{
			// raise ValueChanged event
			EventHandler tmp = this.ValueChanged;
			if ( tmp != null )
				tmp(this, EventArgs.Empty);
		}
		#endregion

		#endregion

		#region Private Members

		#region Fields
		private SelectedWebServiceMethod _selectedWSM;
		#endregion

		#region Methods
		private void showSelectedWebServiceMethod()
		{
			this.SuspendLayout();
			if ( _selectedWSM == null || _selectedWSM.WebService == null ) {
				// clear view
				txtWebService.ForeColor = SystemColors.GrayText;
				txtWebService.TextAlign = HorizontalAlignment.Center;
				txtWebService.Text = "Please Select A WebService";
				cmbWebMethod.Items.Clear();
				lblURL.Text = "";
				//
			} else {
				// show webservice name 
				txtWebService.ForeColor = SystemColors.ControlText;
				txtWebService.TextAlign = HorizontalAlignment.Left;
				txtWebService.Text = _selectedWSM.WebService.Name;
				// fill webmethod combobox and 
				// select first webmethod or if available the selected webmethod
				cmbWebMethod.Items.AddRange(_selectedWSM.WebService.GetWebMethods().ToArray());
				if ( _selectedWSM.WebMethod != null &&
					 cmbWebMethod.Items.Contains(_selectedWSM.WebMethod) ) {
					// set selected webmethod
					cmbWebMethod.SelectedItem = _selectedWSM.WebMethod;
				} else if ( cmbWebMethod.Items.Count > 0 ) {
					// select first web method
					cmbWebMethod.SelectedIndex = 0;
				}
				// show webservice url
				lblURL.Text = string.Format("URL: {0}", _selectedWSM.WebService.Uri);
			}
			this.ResumeLayout(true);
		}
		#endregion

		#region EventHandlers
		private void cmbWebMethod_SelectedIndexChanged(object sender, EventArgs e)
		{
			if ( _selectedWSM != null ) {
				_selectedWSM.WebMethod = cmbWebMethod.SelectedItem as Repository.WebMethod;
			}
		}

		#endregion

		#endregion
	}
}
